<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/4/20
 * Time: 16:16
 */
namespace app\admin\controller;
use think\Config;
use think\Controller;
use think\Validate;

/**
 * -----------------------------------
 * Class Base
 * @package app\admin\controller
 * 主要格式化返回信息,和数据验证类
 * ----------------------------------
 */
class Base extends Controller{
    /**
     * ajax请求返回成功信息
     * @param  string $data
     * @return array
     */
    public static function formatSuccessResult($data = null){
        return json(['code' => Config::get('error_table.success')['code'],'msg' => Config::get('error_table.success')['msg'],'data' => $data]);
    }
    /**
     * ajax请求返回错误信息
     * @param int $code
     * @param string $errorMsg
     * @param string $data
     * @return array
     */
    public static function formatResult($code, $errorMsg, $data = null){
        return json(['code' => $code,'msg' => $errorMsg,'data'=>$data]);
    }

    /**
     * @param array $rule 验证规则
     * @param array $message 验证信息
     * @param array $post  验证数据
     * @return bool  返回状态
     */
    public static function validateData($rule = [],$message = [],$post = []){
        $validate = new Validate($rule,$message);
        $validate_result   = $validate->check($post);
        if(!$validate_result){
            throw new \LogicException($validate->getError(),Config::get('error_table.validate')['code']);/*返回验证结果*/
        }
        return true;
    }
}